package hip.util.poi;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qiujl
 * @date 2016-3-2
 * @describe 通用poi导入导出接口服务
 */

public class HxlsRead extends HxlsAbstract {
    //数据处理解析数据的接口
    private HxlsOptRowsInterface hxlsOptRowsInterface;
    //处理数据总数
    private int optRows_sum = 0;
    //处理数据成功数量
    private int optRows_success = 0;
    //处理数据失败数量
    private int optRows_failure = 0;
    //excel表格每列标题
    private List<String> rowtitle;
    //失败数据
    private List<List<String>> failrows;
    //失败原因
    private List<String> failmsgs;

    //要处理数据所在的sheet索引,sheet索引从0开始
    private int sheetIndex;

    private Object obj;

    /**
     * 导入文件的名称
     *
     * @param filename             导入文件的物理路径
     * @param sheetIndex           要读取数据所在sheet序号
     * @param hxlsOptRowsInterface 处理读取每一行数据的接口
     * @throws IOException
     * @throws FileNotFoundException
     * @throws SQLException
     */
    public HxlsRead(String filename, int sheetIndex, HxlsOptRowsInterface hxlsOptRowsInterface, Object obj) throws IOException,
            FileNotFoundException, SQLException {
        super(filename);
        this.sheetIndex = sheetIndex;
        this.hxlsOptRowsInterface = hxlsOptRowsInterface;
        this.rowtitle = new ArrayList<String>();
        this.failrows = new ArrayList<List<String>>();
        this.failmsgs = new ArrayList<String>();
        this.obj = obj;
    }

    /**
     * 对读取到一行数据进行解析
     */
    @SuppressWarnings("static-access")
    @Override
    public void optRows(int sheetIndex, int curRow, List<String> rowlist) throws Exception {
        //将rowlist的长度补齐和标题一致
        int k = rowtitle.size() - rowlist.size();
        for (int i = 0; i < k; i++) {
            rowlist.add(null);
        }
        if (sheetIndex == this.sheetIndex) {
            optRows_sum++;

            if (curRow == 0) {//记录标题
                rowtitle.addAll(rowlist);
            } else {
                //接口返回的结果是导入数据的结果，有成功，有失败
                String result = null;
                try {
                    result = hxlsOptRowsInterface.saveOptRows(sheetIndex, curRow, rowlist, obj);
                } catch (Exception e) {
                    optRows_failure++;//失败统计数加1
                    //失败数据列表
                    failrows.add(new ArrayList<String>(rowlist));
                    failmsgs.add("第" + (curRow) + "行：" + e.getMessage());
                }

                if (result != null && result.equals(hxlsOptRowsInterface.SUCCESS)) {
                    optRows_success++;
                }
            }

        }


    }

    public long getOptRows_sum() {
        return optRows_sum;
    }

    public void setOptRows_sum(int optRows_sum) {
        this.optRows_sum = optRows_sum;
    }

    public long getOptRows_success() {
        return optRows_success;
    }

    public void setOptRows_success(int optRows_success) {
        this.optRows_success = optRows_success;
    }

    public long getOptRows_failure() {
        return optRows_failure;
    }

    public void setOptRows_failure(int optRows_failure) {
        this.optRows_failure = optRows_failure;
    }


    public List<String> getRowtitle() {
        return rowtitle;
    }

    public List<List<String>> getFailrows() {
        return failrows;
    }

    public List<String> getFailmsgs() {
        return failmsgs;
    }

    public void setFailmsgs(List<String> failmsgs) {
        this.failmsgs = failmsgs;
    }
}
